package properties

import (
	"encoding/json"
	"fmt"
	"strings"
)

func Marshal(v interface{}) ([]byte, error) {
	jsonBytes, err := json.Marshal(v)
	if err != nil {
		return nil, err
	}
	properties := jsonToProperties(jsonBytes)
	return []byte(toString(properties)), nil
}

func toString(object map[string]interface{}) string {
	var s []string
	for k, v := range object {
		s = append(s, fmt.Sprintf("%s=%v\n", k, v))
	}
	return strings.Join(s, "")
}

func jsonToProperties(_json []byte) map[string]interface{} {
	var m map[string]interface{}
	json.Unmarshal(_json, &m)
	return ToPropMap(m)
}
